package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.annotation.DeleteAnnotation;
import com.baizhi.annotation.InsertAnnotation;
import com.baizhi.dao.UserDao;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {
   @Autowired(required = false)
   private UserDao userDao;

    @Override
    @InsertAnnotation
    public Map selectAll(int page, int limit, String userName) {
        if(userName!=null && !userName.equals("")){
            userName="%"+userName+"%";
        }else{
            userName=null;
        }
        int offset=(page-1)*limit;
        List<User> list=userDao.selectAll(offset,limit,userName);
        int count=userDao.selectCount(userName);
        Map map=new HashMap();
        map.put("data",list);
        map.put("count",count);
        map.put("code",0);
        map.put("msg",0);

        System.out.println(list);
        return map;
    }

    @Override
    //增加方法
    @DeleteAnnotation
    public Map insert(User user) {
        int count=userDao.insert(user);
        Map map=new HashMap();
        if(count>0){
            map.put("isInsert",true);
        }else{
            map.put("isInsert",false);
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map delete(int id) {
       int count=userDao.delete(id);
       Map map=new HashMap();
       if(count>0){
           map.put("isDelete","删除成功");
       }else{
           map.put("isDelete","删除失败");
       }
        return map;
    }

    @Override
    //更新方法
    @DeleteAnnotation
    public Map update(User user) {
        int count=userDao.update(user);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","更新成功");
        }else{
            map.put("isUpdate","更新失败");
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map updates(User user) {
        if(user.getUserStatus()==0){
            user.setUserStatus(1);
        }else{
            user.setUserStatus(0);
        }
        int count=userDao.updates(user);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","修改成功");
        }else{
            map.put("isUpdate","修改失败");
        }
        return map;
    }

    @Override
    @DeleteAnnotation
    public Map updatea(User user) {
        int count=userDao.updatea(user);
        Map map=new HashMap();
        if(count>0){
            map.put("isUpdate","修改成功");
        }else{
            map.put("isUpdate","修改失败");
        }
        return map;
    }

    @Override
    @InsertAnnotation
    public User selectOne(int id) {
        return userDao.selectOne(id);
    }


    //批量导出
    @InsertAnnotation
    public Map selectAlls(){

        List list = userDao.selectAlls();
        Workbook workbook= ExcelExportUtil.exportExcel(new ExportParams("用户信息表","user"),User.class,list);
        try {
            workbook.write(new FileOutputStream("C://Users//小龙//Desktop//新建文件夹//user.xlsx"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map map=new HashMap();
        map.put("data",list);
        if(workbook!=null){
            map.put("isxiazai","导出成功");
        }else{
            map.put("isxiazai","导出失败");
        }
        return map;

    }





    @Override
    @InsertAnnotation
    public List<Map> selectSex() {
        List<Map> maps = userDao.selectSex();
        return maps;


    }

    @Override
    @InsertAnnotation
    public List<Map> selectCity() {
        List<Map> maps = userDao.selectCity();
        return maps;
    }

    @Override
    @InsertAnnotation
    public Map selectDate() {
        Map map=new HashMap();
        map.put("one",userDao.selectDate(0,7));
        map.put("two",userDao.selectDate(8,14));
        map.put("three",userDao.selectDate(15,21));
        System.out.println(map+"service");
        return map;
    }



}
